
package com.yunfei.api.smrz;

import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.smrz.IdCardBo;
import com.yunfei.bo.smrz.IdCardType;
import com.yunfei.enums.ResultStatus;
import com.yunfei.enums.ShiMingRenZhengStatus;
import com.yunfei.mapper.aiiidb.ShiMingRenZhengForProductMapper;
import com.yunfei.utils.idcard.AccountValidatorUtil;
import com.yunfei.utils.idcard.IDCardUtil;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 实名认证接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "获取所有证件类型",tags = "获取所有证件类型")
@RestController
@RequestMapping("/api/smrz")
public class ShiMingRenZhengApiController {
    @Autowired
    private ShiMingRenZhengForProductMapper shiMingRenZhengMapper;
    /**
    * @Description: 获取所有证件类型
    * @Author: zhengyunfei
    * @Date: 2019/1/11
    * @time:17:00
    */
    @GetMapping("/getCardType")
    @ApiOperation(value = "获取所有证件类型",notes = "获取所有证件类型",position = 1,response = Object.class)
    public Object getAllCardType(){
        ResultModel resultModel=new ResultModel();
        List<IdCardType> idCardTypeList=shiMingRenZhengMapper.getCardType(null);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(idCardTypeList);
        return resultModel;
    }
    /**
     * @Description: 根据手机号码判断手机号是否在crm已经实名认证
     * 如果手机号在crm对应的客户已经进行了实名认证，无需实名认证
     * 判断手机号是否在crm已经实名认证：
     * 如果下面的sql结果大于0，已实名认证，无需进行实名认证
     * @Author: zhengyunfei
     * @Date: 2019/1/11
     * @time:17:00
     */
    @GetMapping("/smrzAdd")
    @ApiOperation(value = "实名认证",notes = "实名认证",position = 1,response = Object.class)
    public Object isShiMingRenZhengByMobile(IdCardBo idCardBo){
        ResultModel resultModel=new ResultModel();
        //参数格式校验
        String cardType=idCardBo.getCardType();//证件类型
        String cardNo=idCardBo.getCardNo();//证件号码
        String cardPic=idCardBo.getCardPic();//证件照
        if (validParamMethod(resultModel, cardType, cardNo)) return resultModel;
        boolean isShiMingRenZhengFlg=false;//实名认证标识
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cIdentityType",idCardBo.getCardType());
        queryMap.put("cIdentityNo",idCardBo.getCardNo());
        int count=shiMingRenZhengMapper.isEqualCardNo(queryMap);
        if(count>0){
            isShiMingRenZhengFlg=true;
            resultModel.setMessage(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS.getCode());
            return resultModel;
        }else{
            //新客户

        }
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(count);
        return resultModel;
    }
    /**
    * @Description: 实名认证参数校验
    * @Param: [resultModel, cardType, cardNo]
    * @return: boolean
    * @Author: zhengyunfei
    * @Date: 2019/1/16
    * @time:13:28
    */
    private boolean validParamMethod(ResultModel resultModel, String cardType, String cardNo) {
        //校验规则中国护照 9位  1个字母+8个数字   港澳通行证：9位，1个字母+8个数字   台胞证号是8位数字     国外人永久居留证：15位，前三位是字母
        if("0".equals(cardType)){//身份证
            String errorMsg= IDCardUtil.IDCardValidate(cardNo);
            if(!StringUtil.isNullOrEmpty(errorMsg)){
                //身份证格式错误
                resultModel.setMessage(errorMsg);
                resultModel.setCode(ShiMingRenZhengStatus.IDCARD_NO_ERROR.getCode());
                return true;
            }
        }
        if("1".equals(cardType)){//中国护照
            boolean isHuZhao= AccountValidatorUtil.isHuZhao(cardNo);
            if(!isHuZhao){
                resultModel.setMessage(ShiMingRenZhengStatus.HUZHAO_NO_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.HUZHAO_NO_ERROR.getCode());
                return true;
            }
        }
        if("A".equals(cardType)){//港澳通行证
            boolean isGangAoTongXingZheng=AccountValidatorUtil.isGangAoTongXingZheng(cardNo);
            if(!isGangAoTongXingZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.GANGAOTONGXINGZHENG_NO_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.GANGAOTONGXINGZHENG_NO_ERROR.getCode());
                return true;
            }

        }
        if("D".equals(cardType)){//台胞证
            boolean isTaiBaoZheng=AccountValidatorUtil.isTaiBaoZheng(cardNo);
            if(!isTaiBaoZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.TAIBAOZHENG_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.TAIBAOZHENG_ERROR.getCode());
                return true;
            }
        }
        if("J".equals(cardType)){//国外人永久居留证
            boolean isGaiWaiJuZhuZheng=AccountValidatorUtil.isGuoWaiJuZhuZheng(cardNo);
            if(!isGaiWaiJuZhuZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.GAOWAIJUZHUZHENG_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.GAOWAIJUZHUZHENG_ERROR.getCode());
                return true;
            }
        }
        if("7".equals(cardType)){//其他

        }
        return false;
    }
}
